<?php

namespace App\Http\Resources;

use App\Helpers\BaseResource;

class Comment extends BaseResource
{
    public function toArray($request)
    {
        return $this->filterFields([
            'id' => hashid_encode($this->id),
            'user' => User::make($this->user)->show(['avatar', 'name', 'id', 'gender', 'city', 'province']),
            'stick' => $this->stick,
            'is_hot' => (boolean)$this->is_hot,
            'content' => $this->content,
            'time' => $this->created_at->diffForHumans(),
            'vote' => $this->when($this->ckFields('vote'), function () {
                return \Cache::remember('comment-vote', 10, function () {
                    return (int)$this->upvoters()->count();
                });
            }),
            'c_replay' => (string)$this->c_replay,
            'created_at' => (string)$this->created_at,
            'mode'=>$this->commentable_type,
            'item' => $this->whenLoaded('commentable', function () {
                switch ($this->commentable_type) {
                    case \App\Models\Doc::class:
                        return Doc::make($this->commentable)->show(['id', 'title']);
                        break;
                }
            })
        ]);
    }
}
